package models.global;

import models.UUIDModel;
import models.enums.ImageType;
import models.user.User;
import org.hibernate.annotations.Index;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Table;
import java.util.List;

/**
 * 图片
 */
@Entity(name="gl_images")
@org.hibernate.annotations.Table(appliesTo = "gl_images", indexes = {@Index(name="idx_images_type_recid",columnNames = {"image_type", "record_id"})})
public class Image extends UUIDModel {

    /**
     * aliyun路径
     */
    @Column(name = "oss_path", length=300)
    public String ossPath;

    /**
     * 图片类型
     */
    @Enumerated(EnumType.STRING)
    @Column(name="image_type", length = 20)
    public ImageType imageType;

    /**
     * 要连接图片的对象编号
     */
    @Column(name="record_id", length = 32)
    public String recordId;

    public Image(String ossPath,ImageType imageType, String recordId , User user) {
        this.ossPath = ossPath;
        this.imageType = imageType;
        this.recordId = recordId;
        this.belongCompany = user.belongCompany != null ? user.belongCompany : null ;
    }

    public static List<Image> findByRecordId(String recordId){
        return Image.find("recordId = ?",recordId).fetch();
    }
}
